package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import model.ComplaintModel;
import model.UserPermissions;
import view.CustomerCompliantGUI;
import client.CliMessage;


/**
 * This Class is a Customer Complaint Controller which extends AbstractController implements IValidateInput
 * @author Tal
 *
 */

public class CustomerComplaintController extends AbstractController implements IValidateInput
{
		private CustomerCompliantGUI CustomerCompliantView;
		private ComplaintModel ComplaintModel;
		private UserPermissions up;
		
		/**
		 * This is Customer Complaint controller constructor.
		 * @param CustomerCompliantView is a Customer Compliant window GUI instance.
		 * @param ComplaintModel is a Complaint Model instance.
		 */
		public CustomerComplaintController(CustomerCompliantGUI CustomerCompliantView, ComplaintModel ComplaintModel ) 
		{
			super(CustomerCompliantView,ComplaintModel);
			this.CustomerCompliantView = CustomerCompliantView;
			this.ComplaintModel = ComplaintModel;
			this.up = UserPermissions.getInstance(0, null,null);
			CustomerCompliantView.addSendComplaintActionListener(new SendComplaintListener());
			CustomerCompliantView.setAlwaysOnTop(true);
			CustomerCompliantView.setVisible(true);
		}
		
		
		@Override
		public boolean validateInput() 
		{
			String udet = CustomerCompliantView.getDetails();
			
			if(udet.equals(""))
			{		
				CustomerCompliantView.displayWrnMessage("You Must Enter Feedback Details!!!!");
				return false;
			}
			
			return true;
		}
		
		
		
		/**
		 * Inner class that handles when Send Complaint Button Pressed, implements Action listener
		 * @author Tal
		 *
		 */
		
		class SendComplaintListener implements ActionListener
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				if(!validateInput())
					return;
				
				//initialize Complaint Model
				ComplaintModel.setCID(up.getCurrUserID());
				ComplaintModel.setDetails(new String(CustomerCompliantView.getDetails()));
				ComplaintModel.setStatus(0);//status: 0 - unhandeled , 1 - being handeled , 2 - closed
				
				  JOptionPane.showMessageDialog(null, "Thank you for your feedback! \n"
													+ "Our customer will contact you about your addresing within 24 Hours", "Feedback Information", JOptionPane.INFORMATION_MESSAGE);
				//insert complaint to DB
				String details = ComplaintModel.getDetails();
				int status = ComplaintModel.getStatusInt();
				int uid = ComplaintModel.getCID();
				
				String insertQuery = "adu"	+"INSERT INTO customer_complaints "
											+"(uid,details,status) VALUES "
											+"("+uid+",'"+details+"',"+status+")";
											
				sendToServer(insertQuery);
				
				//activate the back button
				CustomerCompliantView.clickBackButton();
			}
		}


		@Override
		public void handleDBRequestSuccess()
		{
			
			
			System.out.println("Feedback sent succefully");
			
		}


		
		
		@Override
		public void handleDBSearchResult(CliMessage dbResult) 
		{	
			//Do nothing.
					
		}

		@Override
		public void handleNullSearchResult() 
		{
			//Do nothing.
		}
	

}
